//
//  XOWMCustomButton.m
//  sigleAPP
//
//  Created by SKL on 2017/11/10.
//  Copyright © 2017年 SKL. All rights reserved.
//

#import "XOWMCustomButton.h"
#import "NSString+category.h"
#import "UIView+category.h"
@interface XOWMCustomButton ()
@property (nonatomic, assign) BOOL isSizeToFit;
@end
@implementation XOWMCustomButton
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        //        [self setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        //        [self setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        self.showLayerBorder = YES;
    }
    return self;
}

- (instancetype)initWithCustomIMG:(NSString *)imageName bound:(CGRect)rect
{
    self = [super init];
    if (self)
    {
        self.bounds = rect;
        self.customIMG.imageName = imageName;
        self.customIMG.frame = self.bounds;
        self.customIMG.center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0);
    }
    return self;
}

- (instancetype)initWithBackIMG:(NSString *)imagePath
{
    self = [super init];
    if (self)
    {
        self.customIMG.imageName = imagePath;
        self.showLayerBorder = YES;
        [self setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:self.customIMG attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:self.customIMG attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
        NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.customIMG attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:self.customIMG attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        self.customIMG.translatesAutoresizingMaskIntoConstraints = NO;
        [self addConstraints:@[top,left,bottom,right]];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title imageName:(NSString *)imageName
{
   self = [super init];
    if (self)
    {
        self.isSizeToFit = YES;
        [self setTitle:title forState:UIControlStateNormal];
        [self.titleLabel setFont:[UIFont systemFontOfSize:12 weight:UIFontWeightLight]];
        CGSize size = [title sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
        [self setBounds:CGRectMake(0, 0, ceil(size.width), size.height)];
        self.customIMG.imageName = imageName;
        if (self.customIMG.image)
        {
            [self.customIMG yjd_left:0];
            [self.customIMG yjd_centerY:self.center.y];
            [self.customIMG yjd_size:CGSizeMake(size.height, size.height)];
        }
    }
    
    return self;
}

- (instancetype)initWithTitle:(NSString *)title
{
    self = [super init];
    if (self)
    {
        self.isSizeToFit = YES;
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:RGBA(107, 107, 107, 1.0) forState:UIControlStateNormal];
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [self.titleLabel setFont:[UIFont systemFontOfSize:15 weight:UIFontWeightLight]];
    }
    return self;
}

- (XOWMImageView *)customIMG
{
    if (!_customIMG)
    {
        _customIMG = [[XOWMImageView alloc]init];
        [self addSubview:_customIMG];
    }
    return _customIMG;
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
    [super setTitle:title forState:state];
    if (_isSizeToFit)
    {
        CGSize size = [title sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
        self.bounds = CGRectMake(0, 0, ceil(size.width), ceil(size.height));
    }
}

- (void)setShowLayerBorder:(BOOL)showLayerBorder
{
    if (_showLayerBorder != showLayerBorder)
    {
        _showLayerBorder = showLayerBorder;
        self.layer.masksToBounds = YES;
        if (_showLayerBorder)
        {
            self.layer.cornerRadius = 5.0;
            //            self.layer.borderColor  = [UIColor lightGrayColor].CGColor;
            //            self.layer.borderWidth = 0.5;
        }
        else
        {
            self.layer.cornerRadius = 0.0;
            //            self.layer.borderColor  = [UIColor clearColor].CGColor;
            //            self.layer.borderWidth = 0.0;
        }
        
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    
    if (self.state == UIControlStateHighlighted)
    {
        self.alpha = 0.5;
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    if (self.state != UIControlStateHighlighted)
    {
        self.alpha = 1;
    }
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */



- (void)sp_didUserInfoFailed {
    NSLog(@"Continue");
}
@end
